var module = require('./lib/module.Base');
$(document).ready(function($) {
    var HTML_template = {
        orderStatus:'<a href="#tab1" class="tab-link active button">' +
                        '<span>全部</span><span class="num-icon"><%= count %></span>' +
                    '</a>' +
                    '<a href="#tab2" class="tab-link button">待发货</a>' +
                    '<a href="#tab3" class="tab-link button">待收货</a>' +
                    '<a href="#tab4" class="tab-link button">待付款</a>' +
                    '<a href="#tab5" class="tab-link button">已完成</a>',
        orderList:'<% _.forEach(data, function(order) { %>' +
        '<div class="order-box bgf mt5h"><input type="hidden" value="<%= order.id %>" name="id">' +
        '<div class="borderB load-order-detail"><div class="content-padded"><div class="row pt5h"><div class="col-50 c3 fs75">订单编号<%= order.id %></div><div class="col-50 tar c6 fs65">下单：<%= order.createTime %></div></div></div></div>' +
        '<div class="orderP-box borderB mt25h">' +
        '<% _.forEach(order.item, function(item) { %><div class="fs75 borderB"><div class="content-padded"><div class="row no-gutter"><div class="col-50 c9"><p class="margin0 c3 fs75"><%= item.productName %></p><p class="fs65 c9 margin0"><%= _.join(item.attribute,\' \') %></p></div><div class="col-50 c3 tar"><p class="margin0 c3 fs75">￥<span><%= item.price2 %></span></p><p class="fs65 c9 margin0">×<span><%= item.quantity %></span><%= item.unit %></p></div></div></div></div><% }); %>' +
        '<div class="fs75 borderB tar"><div class="content-padded"><p class="fs65">合计：￥<span class="fs75"><%= order.totalAmount %></span><span class="c9 fs65 ml1">| 省下<span><%= order.province %></span>元</span><span class="tdl c9 ml5h">(运费：<%= order.postFee %>元)</span></p></div></div>' +
        '<div class="content-padded fs75"><div class="row" style="overflow: visible; min-height: 1.35rem"><div class="col-50">&nbsp;</div>' +
        '<div class="col-25"><% if(order.workflowStatus == "WAIT_SELLER_SHIP") { %><a href="javascript:void(0);" class="button button-fill red-null-btn cancel-order">取消定单</a><%}else if(order.workflowStatus == "WAIT_BUYER_PAY") {%><a href="javascript:void(0);" class="button button-fill red-null-btn online-pay">在线支付</a><%}else if(order.workflowStatus == "HAVE_BEEN_ISSUED") {%><a href="javascript:void(0);" class="button button-fill red-null-btn deal-pay">确认收货</a><%}else{%>&nbsp;<%}%></div>' +
        '<div class="col-25 rel"><a href="javascript:void(0);" class="button button-fill h-null-btn after-service">售后服务</a><div class="shpiaof"><a href="javascript:void(0);" class="break-pay">坏果赔付</a><a href="tel:023-70621128">023-70621128</a><a href="javascript:void(0);" class="border0 close-btn-menu">取消</a></div></div>' +
        '</div></div></div></div>' +
        '<% }); %>',
        orderDetailContent:'<div class="consignee-box borderB bgf"><div class="bg"></div><div class="content-padded fs75 borderB pb5h"><div class="row no-gutter"><div class="col-20 c9">收货人:</div><div class="col-80 c3"><p class="tar margin0"><%= drawerName %>  <%= drawerNumber %></p></div></div><div class="row no-gutter"><div class="col-25 c9">收货地址:</div><div class="col-75 c3"><p class="tar margin0"><%= address %></p></div></div></div></div>' +
        '<div class="orderP-box borderB borderT bgf mt25h">' +
        '<% _.forEach(products, function(item,key) { %><div class="content-padded fs75 <% if(key == 0){ %> borderB pb5h<% } %>"><div class="row no-gutter"><div class="col-50 c9"><p class="margin0 c3 fs75"><%= item.productName %></p><p class="fs65 c9 margin0"><%= _.join(item.attribute,\' \') %></p></div><div class="col-50 c3 tar"><p class="margin0 c3 fs75">￥<span><%= item.price2 %></span></p><p class="fs65 c9 margin0">×<span><%= item.quantity %></span><%= item.unit %></p></div></div></div><% }); %>' +
        '<div class="fs75 borderB tar"><div class="content-padded"><p class="fs65">合计：￥<span class="fs75"><%= totalAmount %></span><span class="c9 fs65 ml1">| 省下<span><%= province %></span>元</span><span class="tdl c9 ml5h">(运费：<%= postFee %>元)</span></p></div></div>' +
        '<div class="content-padded fs75"><div class="row" style="overflow: visible; min-height: 1.35rem"><div class="col-50">&nbsp;</div>' +
        '<div class="col-25"><% if(workflowStatus == "WAIT_SELLER_SHIP") { %><a href="javascript:void(0);" class="button button-fill red-null-btn cancel-order">取消定单</a><%}else if(workflowStatus == "WAIT_BUYER_PAY") {%><a href="javascript:void(0);" class="button button-fill red-null-btn online-pay">在线支付</a><%}else if(workflowStatus == "HAVE_BEEN_ISSUED") {%><a href="javascript:void(0);" class="button button-fill red-null-btn deal-pay">确认收货</a><%}else{%>&nbsp;<%}%></div>' +
        '<div class="col-25 rel"><a href="javascript:void(0);" class="button button-fill h-null-btn after-service">售后服务</a><div class="shpiaof"><a href="javascript:void(0);" class="break-pay">坏果赔付</a><a href="tel:023-70621128">023-70621128</a><a href="javascript:void(0);" class="border0 close-btn-menu">取消</a></div></div>' +
        '</div></div></div></div>' +
        '<div class="mt25h bgf"><div class="content-padded fs75"><div class="row no-gutter borderB"><div class="col-50"><p class="c3 fs75 margin0 pt5h pb5h">订单编号：</p></div><div class="col-50 tar"><p class="c3 fs75 margin0 pt5h pb5h"><%= id %></p></div></div><% if(!_.isEmpty(yjfCode)){ %><div class="row no-gutter borderB"><div class="col-50"><p class="c3 fs75 margin0 pt5h pb5h">易极付交易号：</p></div><div class="col-50 tar"><p class="c3 fs75 margin0 pt5h pb5h"><%= yjfCode %></p></div></div><% } %><div class="row no-gutter"><div class="col-50"><p class="c3 fs75 margin0 pt5h pb5h">下单时间：</p></div><div class="col-50 tar"><p class="c3 fs75 margin0 pt5h pb5h"><%= createTime %></p></div></div></div></div>'
    };
    var localData = {
        order:{},
        orderDetail:{}
    };
    _.extend(module.PageEventBase.prototype,{
        myOrder: function ($page, pageId, result) {
            if (result.status) {
                localData.order = result.data.list;
            }
            $.loadTemplate($page, {
                orderStatus:{count:localData.order.length},
                tab1:{data:localData.order},
                tab2:{data:_.filter(localData.order,{workflowStatus:"WAIT_SELLER_SHIP"})},
                tab3:{data:_.filter(localData.order,{workflowStatus:"WAIT_BUYER_PAY"})},
                tab4:{data:_.filter(localData.order,{workflowStatus:"HAVE_BEEN_ISSUED"})},
                tab5:{data:_.filter(localData.order,{workflowStatus:"CONFIRMATION_OF_RECEIPT"})}
            }, {
                tab1:HTML_template.orderList,
                tab2:HTML_template.orderList,
                tab3:HTML_template.orderList,
                tab4:HTML_template.orderList,
                tab5:HTML_template.orderList,
                orderStatus:HTML_template.orderStatus
            });
            $page.find('.after-service').on('click',function () {
                if($(this).next().is(':hidden')){
                    $('.shpiaof').fadeOut(400);
                    $(this).next().fadeIn(400);
                }
            });
            $page.find('.close-btn-menu').on('click',function () {
                $(this).parent().fadeOut(400);
            });
            $page.find('.load-order-detail').on('click',function () {
                localData.orderDetail = _.filter(localData.order,{id:$(this).prev().val()})[0];
                $.router.load('#orderDetail');
            });
            $('.tab-link').on('click',function () {
                $('.shpiaof').fadeOut(400);
            });

        },
        orderDetail: function ($page, pageId, result) {
            if(_.isEmpty(localData.orderDetail)){
                window.location.href = '/mobile/myOrder.html';
            }else{
                $.loadTemplate($page, {
                    orderDetailContent:localData.orderDetail
                },{orderDetailContent:HTML_template.orderDetailContent});
                $page.find('.after-service').on('click',function () {
                    if($(this).next().is(':hidden')){
                        $('.shpiaof').fadeOut(400);
                        $(this).next().fadeIn(400);
                    }
                });
                $page.find('.close-btn-menu').on('click',function () {
                    $(this).parent().fadeOut(400);
                });
            }

        }
    });
    var EventList = new module.PageEventBase();
    EventList.setResolve({
        myOrder:{status:true,url:'/app/orderList.htm'},
        orderDetail:{status:false,url:''}
    });
    var pageId = $('.page.page-current').attr('id');
    EventList.init($('.page.page-current'),pageId);
});